'use strict';

angular.module('sugarfreeMemberFrontendApp')
.controller('CourseListCtrl', function ($scope, CourseService) {
	$scope.radius = 25;
	var prom = CourseService.list();
	prom.success(function(resp){
		$scope.data = resp.data;
		CourseService.setPrograms(resp.data);
		console.log(resp.data);
	});

	prom.error(function(resp){
		console.log(resp);
	});

})
.service('CourseService', function($http){
	var programs;
	return {
		list : function(){
			return $http.get('/member/packageLesson/query');
		},

		setPrograms: function(data){
			programs = data;
		},

		getPrograms: function(){
			return programs;
		},

		findById : function(pid){
			var wrapper;
			angular.forEach(programs, function(prog){
				angular.forEach(prog.packageLessons, function(item){
					if(item.id === pid){
						console.log('found');
						wrapper= {course: item, gym: prog.gym};
					}
				});
			});

			return wrapper;
		}
	};
})
.controller('ReceiptCtrl', function($scope, $stateParams, $state, toaster, OauthService){

	$scope.course = $stateParams;

	OauthService.getWithSession('/member/coupon/query?gymId=' + $stateParams.gym)
	.success(function(resp){
		var coupons = resp.data;
		$scope.course.derate = coupons && coupons.length ? coupons[0].beans : 0;
	});

	$scope.book= function(courseId){

		if(4 == $stateParams.type){
		
			OauthService.getWithSession('/member/packageLesson/book?id=' + courseId)
			.success(function(resp){
				if(resp.errCode === '8000'){

					toaster.pop('success', '成功购买课程');
					$state.go('mine', {status: 2});

				} else {
					toaster.pop('error', resp.errMsg);

				}
				console.log(resp);
			});

		} else {

			OauthService.getWithSession('/member/lesson/book?id='+courseId)
			.success(function(resp){
				if(resp.errCode === '8000'){

					toaster.pop('success', '成功购买课程');
					if(3==$stateParams.type){

						$state.go('mine', {status: 8});
					}

					if(2==$stateParams.type){

						$state.go('mine', {status: 4});
					}

				} else {

					toaster.pop('error', resp.errMsg);
				}
				console.log(resp);
			});

					/*
					 */
		}

	};
})
.controller('CourseDetailCtrl', function ($scope, $http, $state, $uibModal, toaster, CourseService,OauthService, programId, gymId) {

	var program = CourseService.findById(programId);
	if(!program){
		$http.get('/member/packageLesson/get?id='+programId)
			.success(function(resp){
				console.log(resp);
				$scope.course = resp.data;
			});

		$http.get('/member/gym/get?id=' + gymId)
			.success(function(resp){
				console.log(resp);
				$scope.gym = resp.data;
			});
	} else{
		$scope.course = program.course; 
		$scope.gym = program.gym;
	}

	$scope.subscribe = function(){
		if(! OauthService.getSession()){
			OauthService.login('courseS'+programId+'SgymS'+gymId);
		} else {
			$state.go('receipt', {
				id: 		$scope.course.id,
				title:	$scope.course.title,
				price: 	$scope.course.price,
				begin:	$scope.course.lessons[0].beginTime,
				gym: 		$scope.course.gymId, type:4})
		}
	};

	/*
$scope.openSubscribeModal = function(course){

		console.log(course);

		if(! OauthService.getSession()){
			//$state.go('login');
			OauthService.login('courseS'+programId+'gymS'+gymId);
		} else{
			var modal = $uibModal.open({
				templateUrl:'receipt.html',
				controller:'ReceiptCtrl',
				resolve: {
					course:course,
				},
				size:'md'
			});

			modal.result.then(function(){
				var promise = OauthService.getWithSession('/member/packageLesson/book?id='+course.id);
				promise.success(function(resp){
					if ('8000'=== resp.errCode){
						toaster.pop('success', '预订成功' , resp);
					} else {
						toaster.pop('danger', '预订失败' , resp);
					}
				});
			});
		}
	
	};	 */
	

})
.controller('LessonDetailCtrl', function ($scope, $http, $state, $uibModal, toaster, CourseService,OauthService, lessonId) {

	$http.get('/member/lesson/get?id=' + lessonId)
	.success(function(resp){
		$scope.lesson = resp.data;
		
		$http.get('/member/gym/get?id='+$scope.lesson.course.gymId)
		.success(function(resp){
			$scope.gym = resp.data;
		})
	});
		
	$scope.subscribe = function(){
		if(! OauthService.getSession()){
			OauthService.login('lessonS'+lessonId);
		} else {
			$state.go('receipt', 
					{
						id: 		$scope.lesson.id, 
						title: 	$scope.lesson.course.title,
						price: 	$scope.lesson.course.price, 
						begin: 	$scope.lesson.beginTime, 
						gym:   	$scope.lesson.course.gymId, 
						type:  	$scope.lesson.course.classType.value
					})
		}
	};

});
